package com.open.capacity.salary.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.salary.entity.GzRecordSalary;
import com.open.capacity.salary.service.GzRecordSalaryService;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.SysUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.*;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.salary.entity.GzEnableMonth;
import com.open.capacity.salary.dao.GzEnableMonthDao;
import com.open.capacity.salary.service.GzEnableMonthService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;


@Service
public class GzEnableMonthServiceImpl implements GzEnableMonthService {

    @Autowired
    private GzEnableMonthDao gzEnableMonthDao;

    @Autowired
    private GzRecordSalaryService salaryService;

    /**
     * 添加
     *
     * @param gzEnableMonth
     */
    public int save(GzEnableMonth gzEnableMonth) {
        SysUser loginUser = SysUserUtil.getLoginAppUser();

        GzEnableMonth exists = gzEnableMonthDao.findByZtId(gzEnableMonth.getCompanyId(), gzEnableMonth.getZtId());
        if (exists == null) {
            gzEnableMonth.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            gzEnableMonth.setCurrentMonth(gzEnableMonth.getStartMonth());
            gzEnableMonth.setCreateTime(new Date());
            gzEnableMonth.setCreateUserid(loginUser.getId());
            gzEnableMonth.setCreateUsername(loginUser.getRealName());
            gzEnableMonthDao.save(gzEnableMonth);
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改
     *
     * @param gzEnableMonth
     */
    public int update(GzEnableMonth gzEnableMonth) {
        gzEnableMonth.setCreateTime(new Date());
        return gzEnableMonthDao.update(gzEnableMonth);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int next(String companyId, String ztId) {
        GzEnableMonth model = gzEnableMonthDao.findByZtId(companyId, ztId);

        //生成下一个月数据
        salaryService.nextMonth(companyId, ztId, model.getCurrentMonth());

        model.setCurrentMonth(DateUtil.getNextMonth(model.getCurrentMonth()));
        gzEnableMonthDao.update(model);

        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int pre(String companyId, String ztId) {
        GzEnableMonth model = gzEnableMonthDao.findByZtId(companyId, ztId);

        //清空本月数据
        salaryService.preMonth(companyId, ztId, model.getCurrentMonth());

        String newMonth = DateUtil.getPreMonth(model.getCurrentMonth());
        Assert.isTrue(newMonth.compareTo(model.getStartMonth()) >= 0, "当前月份已经是启用月份，不允许再反月结");
        model.setCurrentMonth(newMonth);
        gzEnableMonthDao.update(model);



        return 1;
    }

    @Override
    public GzEnableMonth findByZtIdWithoutException(String companyId, String ztId) {
        GzEnableMonth enableMonth = gzEnableMonthDao.findByZtId(companyId, ztId);
        return enableMonth;
    }

    @Override
    public GzEnableMonth findByZtId(String companyId, String ztId) {
        GzEnableMonth enableMonth = gzEnableMonthDao.findByZtId(companyId, ztId);
        Assert.isTrue(enableMonth != null, "该账套未初始化启用月份！");
        return enableMonth;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<GzEnableMonth> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<GzEnableMonth> list = gzEnableMonthDao.findAll(params);
        PageInfo<GzEnableMonth> pageInfo = new PageInfo(list);

        return PageResult.<GzEnableMonth>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<GzEnableMonth> findAll(Map<String, Object> params) {
        return gzEnableMonthDao.findAll(params);
    }

}
